﻿using System;
using System.Collections.Generic;
using Web7.Core.Apps;
using Web7.FilterProviders.FilterCriterion;
using Web7.Apps.Account.Controllers;
using Web7.Core.ActionFilters.Authorization;
using Web7.Core.Security.Users;
using Web7.Apps.Account.Services;
using Web7.Core.Security;
using System.Web.Security;

namespace Web7.Apps.Account
{
    public class AccountApp : App, IAppMetaData//, IAuthenticationApp
    {
        public override string AppName
        {
            get { return "Account"; }
        }

        public override string AuthenticationAppName
        {
            get { return AppName; }
        }

        public override void RegisterRoutes()
        {
            MapRoute("Account.Index",
              "Account/Account/Index",
              new { controller = "Account", action = "Index",page=1 },
              new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.IndexPage",
                "Account/Account/Index/{page}",
                new { controller = "Account", action = "Index"},
                new{page=@"\d+"},
                new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.Logon",
                "Account/Logon",
                new { controller = "Account", action = "Logon" },
                null,
                new string[] { "Web7.Apps.Account.Controllers" });

            #region User Route
            MapRoute("Account.GetUser",
                    "Account/Account/{id}",
                    new { controller = "Account", action = "GetUser" },
                    new { id = @"\d+" },
                    new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.UserCrt",
                "Account/Account/UserCrt",
                new { controller = "Account", action = "UserCrt" },
                null,
                new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.Edt",
                "Account/Account/Edt/{id}",
                new { controller = "Account", action = "UserEdt" },
                null,
                new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.UserDel",
              "Account/Account/Del/{id}",
              new { controller = "Account", action = "UserDel" },
              null,
              new string[] { "Web7.Apps.Account.Controllers" });

            #endregion

            #region Role Route
            MapRoute("Account.Role",
                "Account/Account/Role",
                new { controller = "Account", action = "Role" },
                null,
                new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.Role.RoleEdit",
                "Account/Account/Role/{id}",
                new { controller = "Account", action = "RoleEdt" },
                new { id = @"\d+" },
                new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.Role.RoleDel",
                "Account/Account/RoleDel/{id}",
                new { controller = "Account", action = "RoleDel" },
                new { id = @"\d+" },
                new string[] { "Web7.Apps.Account.Controllers" });

            MapRoute("Account.Role.RoleCrt",
                "Account/Account/RoleCrt",
                new { controller = "Account", action = "RoleCrt" },
                null,
                new string[] { "Web7.Apps.Account.Controllers" });
            #endregion
        }

        public List<AppMenuItem> GetAppMenus()
        {
            throw new NotImplementedException();
        }

        public List<PermissionInput> GetAppPermissions()
        {
            throw new NotImplementedException();
        }

        public override void RegisterFilters(FilterProviders.FilterRegistryFilterProvider filterRegistry, System.Web.Mvc.GlobalFilterCollection globalFilters)
        {
        //    ControllerActionFilterCriteria adminSubMenuCriteria = new ControllerActionFilterCriteria();
        //    adminSubMenuCriteria.AddMethod<AccountController>(s => s.RoleCrt()); //todo:"()"研究一下,有点问题。
        //    adminSubMenuCriteria.AddMethod<AccountController>(s => s.RoleDel(0));
        //    filterRegistry.Add(new[] { adminSubMenuCriteria }, new AdvancedAuthorizationFilter(DependencyResolverContext, user => user.HasPermission("角色管理")));
        }

        //#region IAuthenticationApp 成员

        //public Core.Security.IUser GetUser(System.Web.Routing.RequestContext context)
        //{
        //    if (context.HttpContext.User.Identity.IsAuthenticated)
        //    {
        //        IUserService userService = DependencyInjector.GetService<IUserService>();
        //        if (userService != null)
        //        {
        //            string[] nameWithModule = context.HttpContext.User.Identity.Name.Split('|');
        //            if (nameWithModule.Length == 2)
        //            {
        //                if (nameWithModule[0] == this.AppName)
        //                    return userService.Get(u => u.Username == nameWithModule[1] && u.Status == UserStatus.Normal);
        //            }
        //        }
        //    }
        //    return new UserAnonymous();
        //}

        //public string GetRegistrationUrl(System.Web.Routing.RequestContext context)
        //{
        //    return string.Empty;
        //}

        //public string GetSignInUrl(System.Web.Routing.RequestContext context)
        //{
        //    return FormsAuthentication.LoginUrl;
        //}

        //public string GetSignOutUrl(System.Web.Routing.RequestContext context)
        //{
        //    return string.Empty;
        //}

        //#endregion
    }
}
